﻿#ifdef LV_LVGL_H_INCLUDE_SIMPLE
#include "lvgl.h"
#else
#include "lvgl/lvgl.h"
#endif
#include "lvgl_app/lvgl_app.h"

static lv_obj_t* current_main_cont;

static lv_style_t lab_style_1;/*lab样式1*/
static lv_style_t bar_style_bg1;
static lv_style_t bar_style_indic1;

static void label_event_cb(lv_event_t* e);/*本文件的lab统一的回调*/
static void bar_event_cb(lv_event_t* e);
static void air_lab_event_cb(lv_event_t* e);
static void img_event_cb(lv_event_t* e);
static void weather_lab_event_cb(lv_event_t* e);
static void info_lab_event_cb(lv_event_t* e);

void lv_weather_main(void)
{
    /*
        首先初始化本页面用到的样式
    */

    /*设置lab1的样式*/
    lv_style_init(&lab_style_1);
    lv_style_set_radius(&lab_style_1, 5);
    lv_style_set_bg_opa(&lab_style_1, LV_OPA_COVER);
    lv_style_set_bg_color(&lab_style_1, lv_palette_lighten(LV_PALETTE_GREEN, 1));
    lv_style_set_border_color(&lab_style_1, lv_palette_lighten(LV_PALETTE_GREEN, 1));
    lv_style_set_pad_bottom(&lab_style_1, 3);
    lv_style_set_pad_left(&lab_style_1, 8);
    lv_style_set_pad_right(&lab_style_1, 8);

    /*设置bar1的样式*/
    lv_style_init(&bar_style_bg1);
    lv_style_set_border_color(&bar_style_bg1, lv_palette_main(LV_PALETTE_GREY));
    lv_style_set_border_width(&bar_style_bg1, 2);/*设置外边框的像素粗细*/
    lv_style_set_pad_all(&bar_style_bg1, 4);     /*设置内部填充的像素粗细*/
    lv_style_set_radius(&bar_style_bg1, 6);

    lv_style_init(&bar_style_indic1);
    lv_style_set_bg_opa(&bar_style_indic1, LV_OPA_COVER);
    lv_style_set_radius(&bar_style_indic1, 3);


    /*
        再初始化本页面用到的控件
    */

    /* 创建一个铺满屏幕的对象 */
    current_main_cont = lv_obj_create(lv_scr_act());
    lv_obj_clear_flag(current_main_cont, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_remove_style_all(current_main_cont);
    lv_obj_set_size(current_main_cont, lv_pct(100), lv_pct(100));

    //显示时间
    lv_obj_t* time_lab = lv_label_create(current_main_cont);
    lv_label_set_long_mode(time_lab, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(time_lab, &font_style_DigifaceWide_98, LV_PART_MAIN);
    lv_obj_align(time_lab, LV_ALIGN_CENTER, 0, -80);
    lv_label_set_recolor(time_lab, true);
    lv_label_set_text(time_lab, "88:#C2A970 88");

    //显示城市
    lv_obj_t* city_lab = lv_label_create(current_main_cont);
    lv_obj_align(city_lab, LV_ALIGN_LEFT_MID, 10, -15);
    lv_obj_add_style(city_lab, &font_style_siyuanheiti_23, LV_PART_MAIN);
    // lv_label_set_text(city_lab, "城市");
    //显示温度，附加在城市下面
    lv_obj_t* temp1_lab = lv_label_create(current_main_cont);
    lv_label_set_long_mode(temp1_lab, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(temp1_lab, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(temp1_lab, city_lab, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_label_set_text(temp1_lab, "最高温度 : 30度");
    //显示空气质量
    // lv_obj_t* air_lab = lv_label_create(current_main_cont);
    // lv_obj_add_style(air_lab, &font_style_siyuanheiti_23, LV_PART_MAIN);
    // lv_obj_add_style(air_lab, &lab_style_1, LV_PART_MAIN);
    // lv_obj_set_style_bg_color(air_lab, lv_palette_lighten(LV_PALETTE_ORANGE, 1), LV_PART_MAIN);
    // lv_obj_set_style_text_color(air_lab, lv_color_black(), LV_PART_MAIN);
    // lv_label_set_text(air_lab, "良");
    // lv_obj_align_to(air_lab, city_lab, LV_ALIGN_OUT_RIGHT_MID, 25, 0);
    // //显示天气信息
    // lv_obj_t* weather_lab = lv_label_create(current_main_cont);
    // lv_obj_add_style(weather_lab, &font_style_siyuanheiti_23, LV_PART_MAIN);
    // lv_obj_add_style(weather_lab, &lab_style_1, LV_PART_MAIN);
    // lv_obj_set_style_bg_color(weather_lab, lv_palette_lighten(LV_PALETTE_TEAL, 1), LV_PART_MAIN);
    // lv_obj_set_style_text_color(weather_lab, lv_color_black(), LV_PART_MAIN);
    // lv_label_set_text(weather_lab, "晴");
    // lv_obj_align_to(weather_lab, air_lab, LV_ALIGN_OUT_RIGHT_MID, 2, 0);

    //显示天气图标
    lv_obj_t* weather_img = lv_img_create(current_main_cont);
    lv_img_set_src(weather_img, LVGL_PATH"/xinzhilog/x1/xz_30x1.png");
    lv_obj_align(weather_img, LV_ALIGN_RIGHT_MID, -20, -5);

    

    //显示日期
    lv_obj_t* data_lab = lv_label_create(current_main_cont);
    lv_label_set_long_mode(data_lab, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(data_lab, &font_style_siyuanheiti_28, LV_PART_MAIN);
    lv_obj_align_to(data_lab, time_lab, LV_ALIGN_OUT_BOTTOM_MID, 0, -5);
    lv_label_set_text(data_lab, "1月1日");

    // lv_obj_t* week_lab = lv_label_create(current_main_cont);
    // lv_label_set_long_mode(week_lab, LV_LABEL_LONG_SCROLL_CIRCULAR);
    // lv_obj_add_style(week_lab, &font_style_siyuanheiti_28, LV_PART_MAIN);
    // lv_obj_align_to(week_lab, data_lab, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
    // lv_label_set_text(week_lab, "周一");

    //显示室外温度
    lv_obj_t* temp2_img = lv_img_create(current_main_cont);
    lv_img_set_src(temp2_img, LVGL_PATH"/icons/temperature.png");
    lv_obj_align(temp2_img, LV_ALIGN_LEFT_MID, 0, 50);
    //显示室内温度
    lv_obj_t* temp3_img = lv_img_create(current_main_cont);
    lv_img_set_src(temp3_img, LVGL_PATH"/icons/temperature.png");
    lv_obj_align(temp3_img, LV_ALIGN_RIGHT_MID, -120, 50);

    //显示室外温度条
    lv_obj_t* temp2_bar = lv_bar_create(lv_scr_act());
    lv_obj_remove_style_all(temp2_bar);
    lv_obj_add_style(temp2_bar, &bar_style_bg1, 0);
    lv_obj_add_style(temp2_bar, &bar_style_indic1, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(temp2_bar, lv_palette_lighten(LV_PALETTE_RED, 1), LV_PART_INDICATOR);
    lv_obj_set_size(temp2_bar, 50, 15);
    lv_bar_set_range(temp2_bar, -10, 55);
    lv_obj_align_to(temp2_bar, temp2_img, LV_ALIGN_OUT_RIGHT_MID, 0, 7);
    lv_bar_set_value(temp2_bar, 100, LV_ANIM_OFF);

    //显示室内温度条
    lv_obj_t* temp3_bar = lv_bar_create(lv_scr_act());
    lv_obj_remove_style_all(temp3_bar);
    lv_obj_add_style(temp3_bar, &bar_style_bg1, 0);
    lv_obj_add_style(temp3_bar, &bar_style_indic1, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(temp3_bar, lv_palette_lighten(LV_PALETTE_PINK, 1), LV_PART_INDICATOR);
    lv_obj_set_size(temp3_bar, 50, 15);
    lv_bar_set_range(temp3_bar, -10, 55);
    lv_obj_align_to(temp3_bar, temp3_img, LV_ALIGN_OUT_RIGHT_MID, 0, 7);
    lv_bar_set_value(temp3_bar, 100, LV_ANIM_OFF);

    //室外温度数据
    lv_obj_t* temp2_lab = lv_label_create(current_main_cont);
    lv_label_set_long_mode(temp2_lab, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(temp2_lab, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(temp2_lab, temp2_bar, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
    lv_label_set_text(temp2_lab, "30℃");

    //室内温度数据
    lv_obj_t* temp3_lab1 = lv_label_create(current_main_cont);
    lv_label_set_long_mode(temp3_lab1, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(temp3_lab1, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(temp3_lab1, temp3_bar, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
    lv_label_set_text(temp3_lab1, "30℃");

    //室外温度提示
    lv_obj_t* temp2_lab2 = lv_label_create(current_main_cont);
    lv_label_set_long_mode(temp2_lab2, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(temp2_lab2, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(temp2_lab2, temp2_bar, LV_ALIGN_OUT_TOP_MID, -7, -3);
    lv_label_set_text(temp2_lab2, "室外温度");

    //室内温度提示
    lv_obj_t* temp3_lab2 = lv_label_create(current_main_cont);
    lv_label_set_long_mode(temp3_lab2, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(temp3_lab2, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(temp3_lab2, temp3_bar, LV_ALIGN_OUT_TOP_MID, -7, -3);
    lv_label_set_text(temp3_lab2, "室内温度");


    //显示室外湿度
    lv_obj_t* humi_img = lv_img_create(current_main_cont);
    lv_img_set_src(humi_img, LVGL_PATH"/icons/humidity.png");
    lv_obj_align_to(humi_img, temp2_img, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
    //显示室内湿度
    lv_obj_t* humi_img2 = lv_img_create(current_main_cont);
    lv_img_set_src(humi_img2, LVGL_PATH"/icons/humidity.png");
    lv_obj_align_to(humi_img2, temp3_img, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);

    //显示湿度条，附加在湿度图标右侧
    lv_obj_t* humi_bar = lv_bar_create(lv_scr_act());
    lv_obj_remove_style_all(humi_bar);
    lv_obj_add_style(humi_bar, &bar_style_bg1, 0);
    lv_obj_add_style(humi_bar, &bar_style_indic1, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(humi_bar, lv_palette_lighten(LV_PALETTE_BLUE, 1), LV_PART_INDICATOR);
    lv_obj_set_size(humi_bar, 50, 15);
    lv_bar_set_range(humi_bar, -10, 55);
    lv_obj_align_to(humi_bar, temp2_bar, LV_ALIGN_OUT_BOTTOM_MID, 0, 30);
    lv_bar_set_value(humi_bar, 100, LV_ANIM_OFF);

    //显示室内湿度条，附加在湿度图标右侧
    lv_obj_t* humi2_bar = lv_bar_create(lv_scr_act());
    lv_obj_remove_style_all(humi2_bar);
    lv_obj_add_style(humi2_bar, &bar_style_bg1, 0);
    lv_obj_add_style(humi2_bar, &bar_style_indic1, LV_PART_INDICATOR);
    lv_obj_set_style_bg_color(humi2_bar, lv_palette_lighten(LV_PALETTE_LIGHT_BLUE, 1), LV_PART_INDICATOR);
    lv_obj_set_size(humi2_bar, 50, 15);
    lv_bar_set_range(humi2_bar, -10, 55);
    lv_obj_align_to(humi2_bar, temp3_bar, LV_ALIGN_OUT_BOTTOM_MID, 0, 30);
    lv_bar_set_value(humi2_bar, 100, LV_ANIM_OFF);


    //显示湿度数值
    lv_obj_t* humi_lab = lv_label_create(current_main_cont);
    lv_label_set_long_mode(humi_lab, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(humi_lab, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(humi_lab, humi_bar, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
    lv_label_set_text(humi_lab, "30%");
    //显示湿度类型为室外湿度
    lv_obj_t* humi_lab2 = lv_label_create(current_main_cont);
    lv_label_set_long_mode(humi_lab2, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(humi_lab2, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(humi_lab2, humi_bar, LV_ALIGN_OUT_TOP_MID, -7, -3);
    lv_label_set_text(humi_lab2, "室外湿度");

    //显示湿度数值
    lv_obj_t* humi2_lab = lv_label_create(current_main_cont);
    lv_label_set_long_mode(humi2_lab, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(humi2_lab, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(humi2_lab, humi2_bar, LV_ALIGN_OUT_RIGHT_MID, 5, 0);
    lv_label_set_text(humi2_lab, "30%");
    //显示湿度类型为室内湿度
    lv_obj_t* humi2_lab2 = lv_label_create(current_main_cont);
    lv_label_set_long_mode(humi2_lab2, LV_LABEL_LONG_SCROLL_CIRCULAR);
    lv_obj_add_style(humi2_lab2, &font_style_siyuanheiti_23, LV_PART_MAIN);
    lv_obj_align_to(humi2_lab2, humi2_bar, LV_ALIGN_OUT_TOP_MID, -7, -3);
    lv_label_set_text(humi2_lab2, "室内湿度");


     
    /* 开始为各类控件订阅主题 */
    lv_msg_subsribe_obj(WEATHER_MAIN_CITY, city_lab, "%s");
    lv_obj_add_event_cb(city_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    // lv_msg_send(WEATHER_MAIN_INFO, "湛江");

    lv_msg_subsribe_obj(WEATHER_MAIN_TEMP, temp1_lab, "最高温度 %s度");
    lv_obj_add_event_cb(temp1_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    //温度和湿度
    lv_msg_subsribe_obj(WEATHER_MAIN_TEMP, temp2_lab, "%s℃");
    lv_obj_add_event_cb(temp2_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    lv_msg_subsribe_obj(WEATHER_MAIN_TEMP, temp2_bar, NULL);
    lv_obj_add_event_cb(temp2_bar, bar_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    int temp1_data = 40;
    lv_msg_send(WEATHER_MAIN_TEMP, &temp1_data);

    lv_msg_subsribe_obj(WEATHER_MAIN_HUMI, humi_lab, "%d%%");
    lv_obj_add_event_cb(humi_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    lv_msg_subsribe_obj(WEATHER_MAIN_HUMI, humi_bar, NULL);
    lv_obj_add_event_cb(humi_bar, bar_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    int humi_data = 50;
    lv_msg_send(WEATHER_MAIN_HUMI, &humi_data);
    //室内温度订阅
    lv_msg_subsribe_obj(WEATHWE_INDOOR_TEMP, temp3_lab1, "%d℃");
    lv_obj_add_event_cb(temp3_lab1, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    lv_msg_subsribe_obj(WEATHWE_INDOOR_TEMP, temp3_bar, NULL);
    lv_obj_add_event_cb(temp3_bar, bar_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    int temp12_data = 27;
    lv_msg_send(WEATHWE_INDOOR_TEMP, &temp12_data);

    lv_msg_subsribe_obj(WEATHER_INDOOR_HUMI, humi2_lab, "%d%%");
    lv_obj_add_event_cb(humi2_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    lv_msg_subsribe_obj(WEATHER_INDOOR_HUMI, humi2_bar, NULL);
    lv_obj_add_event_cb(humi2_bar, bar_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    int humi2_data = 78;
    lv_msg_send(WEATHER_INDOOR_HUMI, &humi2_data);


    // lv_msg_subsribe_obj(WEATHER_MAIN_WEATHER,weather_lab,"%c");
    // lv_obj_add_event_cb(weather_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    // lv_msg_subsribe_obj(WEATHER_INDOOR_HUMI, humi2_bar, NULL);
    // lv_obj_add_event_cb(weather_lab, bar_event_cb, LV_EVENT_MSG_RECEIVED, NULL);



    lv_msg_subsribe_obj(WEATHER_MAIN_TIME, time_lab, "%s");
    lv_obj_add_event_cb(time_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    // lv_msg_send(WEATHER_MAIN_TIME, "12:#C2A970 04");

    // lv_msg_subsribe_obj(WEATHER_MAIN_WEEK, week_lab, "%s");
    // lv_obj_add_event_cb(week_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    // // lv_msg_send(WEATHER_MAIN_WEEK, "周三");

    lv_msg_subsribe_obj(WEATHER_MAIN_DATA, data_lab, "%s");
    lv_obj_add_event_cb(data_lab, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
    // lv_msg_send(WEATHER_MAIN_DATA, "10月1日");


    lv_msg_subsribe_obj(WEATHER_MAIN_IMG, weather_img, LVGL_PATH"/xinzhilog/x1/xz_%dx1.png");
    lv_obj_add_event_cb(weather_img, img_event_cb, LV_EVENT_MSG_RECEIVED, NULL);


}
//创建标签消息订阅
static void label_event_cb(lv_event_t* e)
{
    lv_obj_t* label = lv_event_get_target(e);
    lv_msg_t* m = lv_event_get_msg(e);

    const char* fmt = lv_msg_get_user_data(m);

    if (strstr(fmt, "%s") != NULL)
    {
        /* 字符串填入 */
        const char* v = lv_msg_get_payload(m);
        lv_label_set_text_fmt(label, fmt, v);
        ESP_LOGI("lv_we_main","%s",v);
    }
    else
    {
        /* 数字填入 */

        const char* v = lv_msg_get_payload(m);
        lv_label_set_text_fmt(label, fmt , *v);
        ESP_LOGI("lv_we_main","%s",v);
    }
}
//创建进度条消息订阅
static void bar_event_cb(lv_event_t* e)
{
    lv_obj_t* bar = lv_event_get_target(e);
    lv_msg_t* m = lv_event_get_msg(e);
    const char* v = lv_msg_get_payload(m);

    lv_bar_set_value(bar, *v, LV_ANIM_OFF);
}

//创建图片更新订阅
static void img_event_cb(lv_event_t* e)
{
    lv_obj_t* img = lv_event_get_target(e);
    lv_msg_t* m = lv_event_get_msg(e);
    const char* fmt = lv_msg_get_user_data(m);
    const char* v = lv_msg_get_payload(m);
    char path_data[40];

    sprintf(path_data, fmt, *v);
    lv_img_set_src(img, path_data);
    ESP_LOGI("lv_we_main","%s",v);
}

//创建空气质量标签订阅
static void air_lab_event_cb(lv_event_t* e)
{
    lv_obj_t* label = lv_event_get_target(e);
    lv_msg_t* m = lv_event_get_msg(e);

    const char* fmt = lv_msg_get_user_data(m);
    const char* v = lv_msg_get_payload(m);

    if (strcmp("优", v) == 0)
    {
        lv_obj_set_style_bg_color(label, lv_palette_lighten(LV_PALETTE_GREEN, 1), LV_PART_MAIN);
    }
    else if (strcmp("良", v) == 0)
    {
        lv_obj_set_style_bg_color(label, lv_palette_lighten(LV_PALETTE_GREEN, 2), LV_PART_MAIN);
    }
    else if (strcmp("中", v) == 0)
    {
        lv_obj_set_style_bg_color(label, lv_palette_lighten(LV_PALETTE_ORANGE, 1), LV_PART_MAIN);
    }
    else
    {
        lv_obj_set_style_bg_color(label, lv_palette_lighten(LV_PALETTE_RED, 1), LV_PART_MAIN);
    }
    lv_label_set_text_fmt(label, fmt, v);
}
//创建天气信息订阅
static void weather_lab_event_cb(lv_event_t* e)
{
    lv_obj_t* label = lv_event_get_target(e);
    lv_msg_t* m = lv_event_get_msg(e);

    const char* fmt = lv_msg_get_user_data(m);
    const char* v = lv_msg_get_payload(m);
    lv_label_set_text_fmt(label, fmt, v);
    ESP_LOGI("lv_we_main","%s",v);
}

static void info_lab_event_cb(lv_event_t* e)
{
    lv_obj_t* label = lv_event_get_target(e);
    lv_msg_t* m = lv_event_get_msg(e);

    const char* fmt = lv_msg_get_user_data(m);
    const char* v = lv_msg_get_payload(m);
    lv_label_set_text_fmt(label, fmt, v);
    ESP_LOGI("lv_we_main","%s",v);
}